import _ from 'lodash'
var initialState=[];
function carts(state=initialState,action){
    switch(action.type){
        case "ADD_CART":
            var pos =_.findIndex(state,{id:action.payload.id});
            if(pos!==-1){
                state[pos].quantity=state[pos].quantity+1;
                return [...state]
            }else{
                return [...state,{...action.payload,quantity:1}];
            }
        case "UPDATE":
            console.log(action.amount,action.id)
            var pos =_.findIndex(state,{id:action.id});
            if(pos!==-1){
                state[pos].quantity=action.amount;
            }
            return [...state];
        case "DELETE_CART":
            console.log(action.id)
            var pos=_.remove(state,{id:action.id});
            return [...state];
        //     break;
        default:
            return state;
    }
    
}

export default carts;